package com.ctlele.team.space.widget.view;

import android.animation.Animator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ctlele.team.space.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装FABMenu
 *
 * @author stewiechen
 * @date 2023-04-24
 */
public class FloatingActionButtonMenu extends RelativeLayout {

    private final List<FloatingActionButton> buttons = new ArrayList<>();

    private FloatingActionButton root;

    private boolean isOpen = false;

    public FloatingActionButtonMenu(Context context) {
        super(context);
    }

    public FloatingActionButtonMenu(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatingActionButtonMenu(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public FloatingActionButtonMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        binding();
    }

    private void binding() {
        root = new FloatingActionButton(getContext());
        root.setSize(FloatingActionButton.SIZE_NORMAL);
        root.setImageResource(R.mipmap.icon_plus);
        root.setColorFilter(Color.WHITE);
        // root.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.fab_root)));

        LayoutParams fabParentLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.setLayoutParams(fabParentLayout);

        LayoutParams fabRootLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        fabRootLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        root.setLayoutParams(fabRootLayout);
        root.setVisibility(VISIBLE);
        root.setOnClickListener((view) -> {
            if (isOpen) {
                closeFABMenu();
            } else {
                showFABMenu();
            }
        });

        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view instanceof FloatingActionButton) {
                LayoutParams fabChildLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                fabChildLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                fabChildLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);
                buttons.add((FloatingActionButton) view);
                FloatingActionButton fab = (FloatingActionButton) view;
                fab.setSize(FloatingActionButton.SIZE_NORMAL);
                fab.setVisibility(GONE);
                fab.setLayoutParams(fabChildLayout);
            }
        }

        addView(root);
    }

    private void showFABMenu() {
        isOpen = true;
        root.animate().rotationBy(45);

        float base = getResources().getDimension(R.dimen.dimen_fab_base);
        float plus = getResources().getDimension(R.dimen.dimen_fab_plus);

        for (int i = buttons.size() - 1; i >= 0; i--) {
            FloatingActionButton button = buttons.get(i);
            button.setVisibility(VISIBLE);
            float decr = -base;
            button.animate().translationY(decr);
            base += plus;
        }

        root.setSize(FloatingActionButton.SIZE_NORMAL);
    }

    private void closeFABMenu() {
        isOpen = false;
        root.animate().rotationBy(45);
        for (int i = 0; i < buttons.size(); i++) {
            FloatingActionButton button = buttons.get(i);
            button.animate().translationY(0).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(@NonNull Animator animation) {

                }

                @Override
                public void onAnimationEnd(@NonNull Animator animation) {
                    if (!isOpen) {
                        button.setVisibility(GONE);
                    }
                }

                @Override
                public void onAnimationCancel(@NonNull Animator animation) {

                }

                @Override
                public void onAnimationRepeat(@NonNull Animator animation) {

                }
            });
        }
    }

    public FloatingActionButton getRoot() {
        return root;
    }

    public List<FloatingActionButton> getMenuList() {
        return buttons;
    }

    public boolean setOnClickInButtonWithId(OnClickListener listener, int id) {
        for (FloatingActionButton button : buttons) {
            if (id == button.getId()) {
                button.setOnClickListener(listener);
                return true;
            }
        }
        return false;
    }
}
